<?php


namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;


use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;


class TypeAdsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $count=$request->input("count",2);
        $search=$request->input("search","");
        $request=$request->all();

        //查询数据并分页
        $data=\DB::table("typeads")->orderBy("sort")->where("title","like","%".$search."%")->paginate($count);
       // dd($data);
        return view("admin.ads.index",["data"=>$data,"request"=>$request]);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data=\DB::table("types")->where("pid","0")->get();
        return view("admin.ads.create",['data'=>$data]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {


        $data=$request->except(['_token',"img"]);
        if($data['tid']==999){
            return back();
        }
        $file=$request->file("img");
        if(!$file){//判断是否上传文件
            return back();
        }
        $img_type=['png','jpeg','jpg','gif'];//设置上传类型




        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $img_type)){//判断上传文件是否为图片
            return back();
        }


        $file_path="/Uploads/sys/";//设置文件存储路径
        $extension=$file->getClientOriginalExtension();//获取文件后缀
        $fileName = time().str_random(10).'.'.$extension;//设置随机文件名
        $file->move($file_path, $fileName);//移动文件
<<<<<<< HEAD
        $data['img']="/".$file_path.$fileName;
=======
        $data['img']=$file_path.$fileName;
>>>>>>> eabe77c8cecb0ce36b15871c72fc153d607820a2
        $res=\DB::table("typeads")->insert($data);
        if(!$res){//提交失败返回
            return back();
        }else{
            return redirect('/admin/sys/typeads');
        }


    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $non="";
        $list=\DB::table("typeads")->where("id",$id)->first();
        if($list['type']==0){
            $non=1;
        }else{
            $non=0;
        }
        $res=\DB::table("typeads")->where("id",$id)->update(["type"=>$non]);
        return redirect('/admin/sys/typeads');
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $list=DB::table("typeads")->where("id",$id)->first();
         $tid=$list['tid'];
        $data=\DB::table("types")->where("id",$tid)->first();
        $tname=$data['typename'];
        return view("admin.ads.edit",['list'=> $list,"tid"=>$tid,"tname"=>$tname]);
}
        /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data=$request->except(['_token',"img","_method"]);
        if($data['tid']==999){
            return back();
        }
        $file=$request->file("img");
        if($file){//判断是否上传文件




        $img_type=['png','jpeg','jpg','gif'];//设置上传类型




        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $img_type)){//判断上传文件是否为图片
            return back();
        }




        $file_path="/Uploads/sys/";//设置文件存储路径
        $extension=$file->getClientOriginalExtension();//获取文件后缀
        $fileName = time().str_random(10).'.'.$extension;//设置随机文件名
<<<<<<< HEAD
        $file->move($file_path,$fileName);//移动文件
        $data['img']="/".$file_path.$fileName;
=======
        $file->move($file_path, $fileName);//移动文件
        $data['img']=$file_path.$fileName;

>>>>>>> eabe77c8cecb0ce36b15871c72fc153d607820a2

        $list=\DB::table("typeads")->where("id",$id)->first();
        $filepath=ltrim($list['img'],"/");//获取删除的图片
        // dd($filepath);
        $res=\Storage::disk('public')->delete($filepath);
        // dd($res);


     }






        $res=\DB::table("typeads")->where("id",$id)->update($data);


        if(!$res){//提交失败返回
            return back();
        }else{
            return redirect('/admin/sys/typeads');
        }


    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id,Request $request)
    {
        //进行删除 成功返回1失败返回0
        $res=\DB::table("typeads")->where("id",$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }
}
